package com.example.demo.service;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;

/**
 * 负责处理具体的业务逻辑。 调用 dao 层与数据库交互。
 */
@Service // Spring bean
public class UserService implements IUserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public Integer add(User user) {
        // 调用数据访问层
        Integer newUser = userMapper.insert(user);
        return newUser;
    }

    @Override
    public User get(Integer userId) {
        return userMapper.selectById(userId);
        // .orElseThrow(() -> {
        // throw new IllegalArgumentException("用户不存在");
        // });
    }

    @Override
    public Integer update(User user) {
        return userMapper.updateById(user);
    }

    @Override
    public void delete(Integer userId) {
        userMapper.deleteById(userId);
    }

}
